package com.lh.web.controller.statistics;

import com.lh.common.entity.admin.ActivityShopDetails;
import com.lh.common.entity.admin.DataForm;
import com.lh.common.entity.admin.DataUserFrom;
import com.lh.common.utils.*;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.statistics.DataStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * @author FH
 * @ClassName: DataStatisticsAction
 * @Description: (类作用)
 * @date: 2020-05-25 14:28
 */
@Api(value="DataStatisticsAction",tags={"统计数据"})
@RestController
@RequestMapping("dataStatistics")
public class DataStatisticsAction extends AbstractController {

    @Autowired
    private DataStatisticsService dataStatisticsService;

    /**
     * 查询数据报表统计列表
     */
    @ApiOperation(value = "查询数据报表统计列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "startTime", value = "开始时间", required = false, dataTypeClass= Date.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "结束时间", required = false, dataTypeClass= Date.class)
    })
    @GetMapping("/getDataStatisticsList")
    @RequiresPermissions("dataStatistics:getDataStatisticsList")
    public Result<DataForm> getDataStatisticsList(String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        if(startTime != null && !startTime.equals("")){
            DataForm dataForm = dataStatisticsService.selectDataList(params);
            return Result.ok(dataForm);
        }else {
            DataForm dataForm = dataStatisticsService.selectDataStatisticsList(params);
            return Result.ok(dataForm);
        }
    }

    /**
     * 查询埋点列表
     */
    @ApiOperation(value = "查询埋点列表")
    @ApiImplicitParams({})
    @GetMapping("/getDataPointList")
    @RequiresPermissions("dataStatistics:getDataPointList")
    public Result<DataUserFrom> getDataPointList() {
        List<DataUserFrom> dataPointList = dataStatisticsService.getDataPointList();
        return Result.ok(dataPointList);
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @GetMapping("/export")
    @RequiresPermissions("dataStatistics:export")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "startTime", value = "开始时间 YYYY-MM-DD", required = false, dataTypeClass= Date.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "结束时间 YYYY-MM-DD", required = false, dataTypeClass= Date.class)
    })
    public void export(@RequestParam String startTime, @RequestParam String endTime, HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        DataForm dataForm = dataStatisticsService.selectDataList(params);
        if (dataForm == null) {
            dataForm = new DataForm();
        }
        List<DataForm> vouchersList=new ArrayList<DataForm>();
        vouchersList.add(dataForm);
        ExcelUtilJXL.exportExcelToBrowserForJSP(DataForm.class, vouchersList,"统计数据.csv",response);
    }


    /**
     * 覆盖数据报表统计
     */
    @ApiOperation(value = "覆盖数据报表统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "startTime", value = "开始时间 YYYY-MM-dd", required = false, dataTypeClass= Date.class)
    })
    @GetMapping("/setDataStatistics")
    public Result<DataForm> setDataStatistics(String startTime) {
        dataStatisticsService.setDataStatistics(startTime);
        return Result.ok();

    }
}
